import { confirm, showLoading, hideLoading } from '../../../common/utils/feedback'
import { setCache, getCache, removeCache, clearCache } from '../../../common/utils/cache'

const TEST_CACHE_KEY = 'TEST_CACHE'
const testData = { 'name': '张三', age: 18 }

// 模拟从接口异步获得数据
const mockQueryData = () => {
  return new Promise(resolve => {
    showLoading()
    setTimeout(() => {
      hideLoading()
      resolve(testData)
    }, 3000)
  })
}

Page({
  data: {},
  onLoad () {},
  setCacheTest () {
    setCache(TEST_CACHE_KEY, testData, '2030-11-11 12:00:00')
  },
  async getCacheTest () {
    const data = await getCache(`${TEST_CACHE_KEY}_1`, {
      getDataFn: mockQueryData,
      expiredTime: '2030-12-12 12:00:00'
    })
    confirm(`缓存数据为：${JSON.stringify(data)}`)
  },
  removeCacheTest () {
    removeCache(TEST_CACHE_KEY)
  },
  clearCacheTest () {
    clearCache()
  }
})
